﻿//*****************************************************************************
// (C) Copyright 2010 Tiago Pascoal
// This code is licensed under the Microsoft Public License (Ms-PL) 
// [http://www.microsoft.com/opensource/licenses.mspx#Ms-PL].
//
//*****************************************************************************
using System;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace WorkItemVisualization.Controls.QueryEditorControl
{
    internal static class ResourceHelper
    {
        /// <summary>
        /// Gets a resource from a given assembly.
        /// 
        /// It assumes the assembly is already loaded in the current app domain
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public static Stream GetStreamFromResource(string assemblyName, string resourceName,string fileName)
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (GetAssemblyName(assembly.FullName) == assemblyName)
                {
                    System.Resources.ResourceManager resManager;

                    resManager = new System.Resources.ResourceManager(resourceName, assembly);

                    return resManager.GetStream(fileName);
                }
            }
            return null;
        }

        /// <summary>
        /// Gets a resource that is embebbed in an assembly.
        /// 
        /// Searches for assemblies that are ALREADY loaded in the current domain
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="resourceName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static object GetObjectFromResource(string assemblyName, string resourceName, string fileName)
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (GetAssemblyName(assembly.FullName) == assemblyName)
                {
                    System.Resources.ResourceManager resManager;

                    resManager = new System.Resources.ResourceManager(resourceName, assembly);

                    return resManager.GetObject(fileName);
                }
            }
            return null;
        }


        /// <summary>
        /// Gets an assembly name given it's fully qualified name in the format
        /// 
        /// assemblyname , fully qualified class name 
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        private static string GetAssemblyName(string fullName)
        {
            string[] assemblyNameComponents;

            assemblyNameComponents = fullName.Split(',');

            Debug.Assert(assemblyNameComponents != null);
            Debug.Assert(assemblyNameComponents.Length > 0);

            return assemblyNameComponents[0];
        }
    }
}
